#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from generate_abc import compile_source_files
from arkcompiler_tool import link, gen_pa
from gen_arktsconfig import gen_arktsconfig

def compile_project(args, project_name):
    args.src_root = os.path.join(args.projects_root_path, project_name)
    args.build_root = os.path.join(args.projects_build_path, project_name)
    print('src_root:{}'.format(args.src_root))
    print('build_root:{}'.format(args.build_root))

    args.tmp_arktsconfig_file = os.path.join(args.build_root, 'arktsconfig.json')
    # gen project arktsconfig file
    gen_arktsconfig(args.build_root, args.src_root, args.ets_plugins_path, args.arktsconfig_file, args.tmp_arktsconfig_file)

    abc_files = []
    compile_source_files(args, args.src_root, args.build_root, abc_files)
    modules_static_abc_file = os.path.join(args.build_root, 'modules_static.abc')
    print('modules_static_abc_file:{}'.format(modules_static_abc_file))
    # link
    link(args.arkcompiler_cmd_tool_path, modules_static_abc_file, abc_files)
    # gen modules_static pa
    modules_static_pa_file = os.path.join(args.build_root, 'modules_static.pa')
    gen_pa(args.arkcompiler_cmd_tool_path, modules_static_abc_file, modules_static_pa_file)


def compile_project_with_root_path(args):
    for file in os.listdir(args.projects_root_path):
        input_file_path = os.path.join(args.projects_root_path, file)
        if os.path.isdir(input_file_path):
            print('Found project {}'.format(file))
            compile_project(args, file)


def compile_projects():
    parser = argparse.ArgumentParser()
    parser.add_argument('--arkcompiler_tool_path', help='arkcompiler tool path')
    parser.add_argument('--arktsconfig_file', help='arktsconfig template file')
    parser.add_argument('--projects_root_path', help='projects root path')
    parser.add_argument('--projects_build_path', help='projects build path')

    args = parser.parse_args()

    args.arkcompiler_cmd_tool_path = os.path.join(args.arkcompiler_tool_path, 'clang_x64')
    args.ets_plugins_path = os.path.join(args.arkcompiler_tool_path, 'plugins', 'ets', 'stdlib')

    print('arkcompiler_tool_path:{}'.format(args.arkcompiler_tool_path))
    print('arktsconfig_file:{}'.format(args.arktsconfig_file))
    print('projects_root_path:{}'.format(args.projects_root_path))
    print('projects_build_path:{}'.format(args.projects_build_path))

    compile_project_with_root_path(args)


if __name__ == '__main__':
    compile_projects()
